package net.jsoft.pms.repository.ywfwqd;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class YwfwqdStartupRunner implements CommandLineRunner
{
    @Autowired
    @Qualifier("mysqlJdbcTemplate")
    private JdbcTemplate jdbc;

    private static Map<String, AtomicInteger> counter = new HashMap<String, AtomicInteger>();

    @Override
    public void run(String... args) throws Exception
    {
        System.out.println(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");

        //定义SQL   
        String sql = "SELECT * FROM( SELECT substring_index(ywfwxh,'_',2) colname , max(substring_index(ywfwxh,'_',-1) ) as num FROM  t_dzswj_ywfwqdb  group by colname  ) T WHERE T.colname IN ('BS_MHQX','BS_SBJS','BS_XXFW','BS_ZXBS')";
        List<Map<String, Object>> objecArraytList = jdbc.queryForList(sql);
        for (int i = 0; i < objecArraytList.size(); i++)
        {
            Map<String, Object> obj = (Map<String, Object>) objecArraytList.get(i);
            int num = Integer.parseInt((String) obj.get("num"));
            counter.put((String) obj.get("colname"), new AtomicInteger(num));
        }
    }

    public static String getNum(String name)
    {
        AtomicInteger a = counter.get(name);
        String i = null;
        if (a != null)
        {
            i = String.format("%05d", a.getAndIncrement());
            counter.put(name, a);
        }
        return i;

    }
}
